package com.shimao.celnet.payment.controller.report;

import com.shimao.celnet.payment.form.PPayDayReportForm;
import com.shimao.celnet.payment.report.excel.ExcelDayPayDetail;
import com.shimao.celnet.payment.report.excel.ExcelDayPayRefundDetail;
import com.shimao.celnet.payment.report.excel.ExcelPayDayTotal;
import com.shimao.celnet.payment.report.excel.ExcelUtil;
import com.shimao.celnet.payment.service.BEstateService;
import com.shimao.celnet.payment.service.report.PPayDayReportService;
import com.shimao.celnet.payment.util.MyDateUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/payDayReports")
public class PayDayReportController {

    protected Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private PPayDayReportService localService;


    @Autowired//小区
    private BEstateService bEstateService;


    /**
     * 报表导出页面初始化
     * @param formBean
     * @return
     */
    @RequestMapping("/init")
    public String init(@ModelAttribute PPayDayReportForm formBean, Model model){
        logger.info("报表导出页面初始化--开始");
        String retStr="payDayReport/payDayReport";

        //小区取得
        formBean.setEstateList(bEstateService.getAll());

        //查询日期：昨天日期
        if(StringUtils.isEmpty(formBean.getPayDate())) {
            formBean.setPayDate(MyDateUtil.getPreOneDayYmd());
        }

        model.addAttribute("formBean", formBean);

        logger.info("报表导出页面初始化--结束");

        return retStr;
    }


    /**
     * excel报表下载
     * @param formBean
     * @param response
     * @return
     */
    @RequestMapping("/getExcel")
    public  void  getExcel(@ModelAttribute PPayDayReportForm formBean, HttpServletResponse response) {
        logger.info("excel报表下载--开始");
        Map<String, String> map = new HashMap<String, String>();
        if(StringUtils.isEmpty(formBean.getPayDate())) {
            formBean.setPayDate(MyDateUtil.getPreOneDayYmd());
        }
//        map.put("payDate", formBean.getPayDate());//交易日期
//        map.put("estateName", formBean.getEstateName());//小区

        String estateNm ="";
        formBean = localService.getReportExcel(formBean);

        if(StringUtils.isEmpty(formBean.getEstateSfGuid())){
            estateNm ="世茂物业";
        }else{
            estateNm =bEstateService.getBySfGuid(formBean.getEstateSfGuid()).getEstateName();
        }
        //拼装输出文件名称
        String outFileName =estateNm +"_支付日报_"+ MyDateUtil.StrToDateYmd( formBean.getPayDate())+".xlsx";
        String tempFileName = "payDayReport.xlsx";//excl模版名称

        try {
            List<List<Object>> objsLs=new  ArrayList<>();
            objsLs.add(formBean.getExcelDayTotalList());
            objsLs.add(formBean.getExcelPayDetailList());
            objsLs.add(formBean.getExcelRefundPayList());

            List<Class>clsLs=new  ArrayList<>();
            clsLs.add(ExcelPayDayTotal.class);
            clsLs.add(ExcelDayPayDetail.class);
            clsLs.add(ExcelDayPayRefundDetail.class);
            ExcelUtil.getInstance().exportObj2ExceWinByTemplate(response, map, tempFileName, outFileName,
                    objsLs, clsLs, true);
            logger.info("excel报表下载--结束");
         } catch (Exception e) {
            e.printStackTrace();
            logger.error("导出excel出现异常:", e);
    }
}

}